#!/usr/bin/env python

from optparse import OptionParser
from argparser import ArgParser
import sys

class MassProxyGrabber(object):
    def __init__(self, args):
        # Setting all arguments
        sites = args['sites']
        proxies = args['proxies']
        self.whois = args['whois']
        self.quiet = args['quiet']
        self.output = args['output']
        self.sthreads = args['sthreads']
        self.pthreads = args['pthreads']
        self.test_types = args['test_types']

        # Setting sites file or list
        if sites[0] == 'file':
            self.sites = self.openFile(sites[1]) # File
        else:
            self.sites = sites[1].split(',') # List
        # Setting proxies file or list
        if proxies != None:
            if proxies[0] == 'file':
                self.proxies = self.openFile(proxies[1]) # File
            else:
                self.proxies = proxies[1].split(',') # List
        else:
            self.proxies = None
        
    def openFile(self, file_to_open):
        try:
            return open(file_to_open, 'r')
        except IOError:
            print '\n[-] Error: could not open %s to read from' % (file_to_open)
            sys.exit(1)

if __name__=='__main__':
    # Try to use psyco to maximize speed, ingnore if it's not found
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
        
    # Getting and checking arguments for errors
    ap = ArgParser()
    toexit = False
    for i in ap.errors:
        if ap.errors[i][0] == True:
            if toexit == False:
                toexit = True
            print ap.errors[i][1]
    if toexit == True:
        sys.exit(1)
        
    # Initializing and passing arguments to main class
    mpg = MassProxyGrabber(ap.getArguments())
    
    # Testing
    for i in (mpg.sites, mpg.proxies, mpg.whois, mpg.quiet, mpg.output, mpg.sthreads, mpg.pthreads, mpg.test_types):
        print '%s\n' % (i)
